package com.toune.dltools.widget

import android.content.Context
import android.graphics.Color
import android.os.Build
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import android.widget.RelativeLayout
import androidx.annotation.RequiresApi
import com.toune.dltools.R
import kotlinx.android.synthetic.main.layout_input_edit_text_view.view.*

/**
 * @Author Dong Lei
 * @Date 2020/12/17 0017-上午 10:11
 * @Info 描述：封装的输入框
 */
class DLInputEditTextView : RelativeLayout {
    companion object {
        val RIGHT_ICON_ALWAY_VISiBLE = 0  //右面的图标一直显示
        val RIGHT_ICON_ALWAY_GONE = 1   //不显示(默认)
        val RIGHT_ICON_INPUT_VISiBLE = 2  //输入内容后显示
    }

    constructor(context: Context) : super(context) {
        initView(context)
    }

    constructor(context: Context, attributes: AttributeSet) : super(context, attributes) {
        readAttr(context, attributes)
        initView(context)
    }

    constructor(context: Context, attributes: AttributeSet, int: Int) : super(
        context,
        attributes,
        int
    ) {
        readAttr(context, attributes)
        initView(context)
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        readAttr(context!!, attrs!!)
        initView(context!!)
    }

    private fun readAttr(context: Context, attributes: AttributeSet) {
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attributes, R.styleable.InputEditTextView)
        inputType = obtainStyledAttributes.getInteger(
            R.styleable.InputEditTextView_android_inputType,
            EditorInfo.TYPE_NULL
        )
        if (obtainStyledAttributes.hasValue(R.styleable.InputEditTextView_hint)) {
            hintStr = obtainStyledAttributes.getString(R.styleable.InputEditTextView_hint)!!
        }
        RIGHT_ICON_STATE =
            obtainStyledAttributes.getInteger(R.styleable.InputEditTextView_rightIconShow, 1)
        resourceId = obtainStyledAttributes.getResourceId(
            R.styleable.InputEditTextView_rightIconId,
            R.mipmap.ic_real_name_close
        )
        obtainStyledAttributes.recycle()
    }

    var inputType = EditorInfo.TYPE_CLASS_TEXT
    var hintStr = ""
        set(value) {
            field = value
            mRootView!!.editText.hint = value
        }
    var resourceId = R.mipmap.ic_real_name_close
        set(value) {
            field = value
            if (mRootView != null) {
                setRightIcon()
            }
        }
    var RIGHT_ICON_STATE = RIGHT_ICON_ALWAY_GONE

    var mRootView: View? = null
    private fun initView(context: Context) {
        val view =
            LayoutInflater.from(context).inflate(R.layout.layout_input_edit_text_view, this, true)
        postInvalidate()
        mRootView = view
        view.editText.hint = hintStr
        view.editText.inputType = inputType
        setRightIcon()

        view.rightIv.setOnClickListener {
            val selected = view.rightIv.isSelected
            view.rightIv.isSelected = !selected
            if (onInputEditTextViewClickListener != null) {
                onInputEditTextViewClickListener!!.rightIconClick(view.rightIv)
            }
        }
        view.editText.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (RIGHT_ICON_STATE == RIGHT_ICON_INPUT_VISiBLE) {
                    if (s!!.isNotEmpty()) {
                        view!!.rightIv.visibility = View.VISIBLE
                    } else {
                        view!!.rightIv.visibility = View.GONE
                    }
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })
        view.editText.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                hideError()
//                view!!.lineTv.setBackgroundColor(Color.GREEN)
            } else {
//                view!!.lineTv.setBackgroundColor(Color.GREEN)
            }
            if (onInputEditTextViewClickListener != null) {
                onInputEditTextViewClickListener!!.focusChangeListener(
                    view.editText.text.toString(),
                    hasFocus
                )
            }
        }

        view.editText.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                searchText(view.editText.text.toString())
            }
            return@setOnEditorActionListener true
        }
//        var parm = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
//        addView(view!!,parm)
    }

    private fun setRightIcon() {
        when (RIGHT_ICON_STATE) {
            RIGHT_ICON_ALWAY_GONE -> {
                mRootView!!.rightIv.visibility = GONE
            }
            RIGHT_ICON_ALWAY_VISiBLE -> {
                mRootView!!.rightIv.visibility = VISIBLE
                mRootView!!.rightIv.setImageResource(resourceId)
            }
            RIGHT_ICON_INPUT_VISiBLE -> {
                mRootView!!.rightIv.visibility = GONE
                mRootView!!.rightIv.setImageResource(resourceId)
            }
        }
    }

    fun showError(string: String) {
        if (string.isNotEmpty()) {
            mRootView!!.errTv.visibility = VISIBLE
            mRootView!!.errTv.text = string
        }
    }

    fun hideError() {
        mRootView!!.errTv.visibility = GONE
    }

    fun getInputText(): String {
        return mRootView!!.editText.text.toString()
    }

    fun setInputText(str: String) {
        return mRootView!!.editText.setText(str)
    }

    fun searchText(str: String) {
        if (onInputEditTextViewClickListener != null) {
            onInputEditTextViewClickListener!!.searchListener(str)
        }
    }

    fun clearInput() {
        mRootView!!.editText.setText("")
    }

    var onInputEditTextViewClickListener: OnInputEditTextViewClickListener? = null

    interface OnInputEditTextViewClickListener {
        fun rightIconClick(imageView: ImageView)
        fun focusChangeListener(string: String, hasFocus: Boolean)
        fun searchListener(str: String)
    }

}